package view;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Collection;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;

import controller.PlanningController;

import model.OfferedCourse;
import model.Teacher;
import model.container.TeacherContainer;

public class TaskFrame extends JFrame {
	/* Required for serializable interface */
	private static final long serialVersionUID = 1L;
	/* The controller for this view */
	private PlanningController controller = PlanningController.getInstance();
	/* List of teachers */
	private JComboBox<Teacher> cboTeacher;
	/* Button to request tasks for given teacher */
	private JButton btnGetTasks;
	/* Button to request tasks for all teachers */
	private JButton btnGetTasksAll;
	/* Table to display the tasks*/
	private JTable tblTasks;
	
	/** Constructs a new task frame */
	public TaskFrame() {
		this.setSize(800, 600);
		this.setLayout(new BorderLayout());
		this.init();
		this.loadTeachers();
		this.getTasks(null);
	}
	
	/* Builds  the frame */
	private void init() {
		// Get content pane
		Container pane = this.getContentPane();
		
		// pnStudent
		JPanel pnTeacher = new JPanel(new FlowLayout());
		
		// cboStudent
		this.cboTeacher = new JComboBox<Teacher>();
		pnTeacher.add(this.cboTeacher);
		
		// btnGetSchedule
		this.btnGetTasks = new JButton("Get");
		this.btnGetTasks.addActionListener(new BtnGetTasksClickedListener());
		pnTeacher.add(this.btnGetTasks);
		
		// btnGetScheduleAll
		this.btnGetTasksAll = new JButton("Get all");
		this.btnGetTasksAll.addActionListener(new BtnGetTasksAllClickedListener());
		pnTeacher.add(this.btnGetTasksAll);
		
		// tblSchedule
		this.tblTasks = new JTable();
		JScrollPane scrollPane = new JScrollPane(tblTasks);
		
		// Add components to frame
		pane.add(pnTeacher, BorderLayout.NORTH);
		pane.add(scrollPane, BorderLayout.SOUTH);
	}
	
	/* Gets the tasks for a teacher or for all teachers */
	private void getTasks(Teacher teacher) {
		Collection<OfferedCourse> tasks;
		
		// Get tasks
		if (teacher == null) {
			tasks = this.controller.getTasks();
		} else {
			tasks = this.controller.getTasks(teacher); 
		}
				
		// Set model for table
		TaskModel taskModel = new TaskModel(tasks);
		this.tblTasks.setModel(taskModel);
	}
	
	/* Loads all teachers into the combo box */
	private void loadTeachers() {
		// Clear the combobox
		this.cboTeacher.removeAllItems();
		// Get a reference to the teacher container
		TeacherContainer teacherContainer = TeacherContainer.getInstance();
		// Request a list of teachers
		ArrayList<Teacher> teachers = (ArrayList<Teacher>)teacherContainer.getTeachers();
		// Add to combobox
		for (Teacher teacher : teachers) {
			this.cboTeacher.addItem(teacher);
		}
	}
	
	/** Get button click handler */
	public void BtnGetTasksClickedHandler(ActionEvent e){
		Teacher teacher = (Teacher)this.cboTeacher.getSelectedItem();
		this.getTasks(teacher);
	}
	
	/** Get button click handler */
	public void BtnGetTasksAllClickedHandler(ActionEvent e){
		this.getTasks(null);
	}
	
	/** Get button click listener */
	private class BtnGetTasksClickedListener implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			BtnGetTasksClickedHandler(e);
		}
	}
	
	/** Get button click listener */
	private class BtnGetTasksAllClickedListener implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			BtnGetTasksAllClickedHandler(e);
		}
	}
}